package com.cait.dutyfree.action.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.cait.dutyfree.dataaccess.AirlineServiceDao;
import com.cait.dutyfree.dataaccess.InventoryServiceDAO;
import com.cait.dutyfree.dataaccess.ProductServiceDAO;
import com.cait.dutyfree.pojo.Category;
import com.cait.dutyfree.pojo.Product;
import com.cait.dutyfree.pojo.ShoppingCartItem;
import com.cait.dutyfree.pojo.SubCategory;
import com.cait.dutyfree.pojo.view.ProductDTO;
import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport implements
		ServletContextAware, SessionAware, ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProductServiceDAO productServiceDAO;
	private AirlineServiceDao airlineServiceDao;
	private InventoryServiceDAO inventoryServiceDAO;

	private List<Product> products = new ArrayList<Product>();
	private List<Product> airlines = new ArrayList<Product>();
	private List<Category> categories = new ArrayList<Category>();
	private List<SubCategory> subCategories = new ArrayList<SubCategory>();
	private List<ProductDTO> productDtos = new ArrayList<ProductDTO>();

	// view product details
	private ProductDTO productDto;

	private ServletContext servletContext;
	private HttpServletRequest request;
	private Map<String, Object> session;

	public String showproducts() {
		Integer aid = Integer.valueOf(request.getParameter("airline_id"));
		try {
			List<ProductDTO> pdtos = this.productServiceDAO
					.findProductsDTOByArilineId(aid);
			this.setProductDtos(pdtos);
			this.session.put("airlineId", aid);
			setAirlines(getAirlineServiceDao().getAll());
			this.setCategories(this.inventoryServiceDAO.getCategories());
			this.setSubCategories(this.inventoryServiceDAO.getSubCategories());
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return NONE;
		}

	}

	public String getCategoryProducts() {
		int categoryId = Integer.valueOf(request.getParameter("categoryId"));
		Integer airlineId = (Integer) this.session.get("airlineId");
		try {
			this.setProductDtos(this.inventoryServiceDAO
					.getProductsDTOByCategoryIdAndAirLineId(categoryId,
							airlineId));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return NONE;
		}
	}

	public String getSubCategoryProducts() {
		int categoryId = Integer.valueOf(request.getParameter("categoryId"));
		int subCategoryId = Integer.valueOf(request
				.getParameter("subCategoryId"));
		Integer airlineId = (Integer) this.session.get("airlineId");
		try {
			if (subCategoryId == 0) {
				this.setProductDtos(this.inventoryServiceDAO
						.getProductsDTOByCategoryIdAndAirLineId(categoryId,
								airlineId));
			} else {
				this.setProductDtos(this.inventoryServiceDAO
						.getProductDTOBySubcategoryIdAndAirlineId(
								subCategoryId, airlineId));
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return NONE;
		}
	}

	public String getSubCategoriesByCategory() {
		int categoryId = Integer.valueOf(request.getParameter("categoryId"));
		try {
			ArrayList<SubCategory> subCat = this.inventoryServiceDAO
					.getSubCategoriesByCategory(categoryId);
			if (subCat == null) {
				this.setSubCategories(new ArrayList<SubCategory>());
			} else {
				this.setSubCategories(this.inventoryServiceDAO
						.getSubCategoriesByCategory(categoryId));
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return NONE;
		}
	}

	public String viewProductDetails() {
		Integer productId = Integer.valueOf(request.getParameter("productId"));
		Integer airlineId = (Integer) this.session.get("airlineId");
		try {
			ProductDTO dto = this.productServiceDAO
					.findProductDTOByProductIdAndAirlineId(productId, airlineId);
			System.out.println(dto);
			this.setProductDto(dto);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return NONE;
		}
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProductServiceDAO(ProductServiceDAO productServiceDAO) {
		this.productServiceDAO = productServiceDAO;
	}

	public ProductServiceDAO getProductServiceDAO() {
		return productServiceDAO;
	}

	public AirlineServiceDao getAirlineServiceDao() {
		return airlineServiceDao;
	}

	public void setAirlineServiceDao(AirlineServiceDao airlineServiceDao) {
		this.airlineServiceDao = airlineServiceDao;
	}

	public List getAirlines() {
		return airlines;
	}

	public void setAirlines(List airlines) {
		this.airlines = airlines;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public List<SubCategory> getSubCategories() {
		return subCategories;
	}

	public void setSubCategories(List<SubCategory> subCategories) {
		this.subCategories = subCategories;
	}

	public void setInventoryServiceDAO(InventoryServiceDAO inventoryServiceDAO) {
		this.inventoryServiceDAO = inventoryServiceDAO;
	}

	public void setProductDtos(List<ProductDTO> productDtos) {
		// get all the products in the cart
		if (this.session.containsKey("shoppingCart")
				&& this.session.get("shoppingCart") instanceof List) {
			@SuppressWarnings("unchecked")
			List<ShoppingCartItem> lst = (List<ShoppingCartItem>) this.session
					.get("shoppingCart");
			for (int i = 0; i < lst.size(); i++) {
				ShoppingCartItem item = lst.get(i);
				Product pCart = item.getProduct();
				for (int j = 0; j < productDtos.size(); j++) {
					ProductDTO pdto = productDtos.get(j);
					Integer productId = pdto.getId();
					if (pCart.getId() == productId) {
						// reduct the amount by cart count from product
						pdto.setAvailableCount(pdto.getAvailableCount()
								- item.getCount());
						break;
					}
				}
			}
		}
		this.productDtos = productDtos;
	}

	public List<ProductDTO> getProductDtos() {
		return productDtos;
	}

	public ProductDTO getProductDto() {
		return productDto;
	}

	public void setProductDto(ProductDTO productDto) {
		this.productDto = productDto;
	}

}
